package com.zscat.mallplus.sys.controller;


import com.zscat.mallplus.sys.entity.SysQiniuConfig;
import com.zscat.mallplus.sys.service.ISysQiniuConfigService;
import com.zscat.mallplus.sys.service.impl.OssServiceImpl;
import com.zscat.mallplus.sys.vo.OssCallbackResult;
import com.zscat.mallplus.sys.vo.OssPolicyResult;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.QiNiuUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 七牛相关操作接口
 * https://github.com/shenzhuan/mallplus on 2018/4/26.
 */
@RestController
@Api(tags = "OssController", description = "Oss管理")
@RequestMapping("/qiniu")
public class QIniuController {
    @Resource
    private ISysQiniuConfigService qiNiuService;

    /**
     *
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/upload1")
    Object upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        try {
            SysQiniuConfig config =qiNiuService.getById(2);
            File files = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator")+file.getOriginalFilename());
            file.transferTo(files);
          //  result = QiNiuUtil.fileUpload(files.getAbsolutePath(), file.getOriginalFilename());
            return new CommonResult().success(QiNiuUtil.fileUpload(files,file.getOriginalFilename(),config));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new CommonResult().failed();
    }
}
